package com.zxp.server.util.token;

import com.zxp.server.service.impl.UserServiceImpl;
import com.zxp.server.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class tokenService {

    @Autowired
    private UserServiceImpl userService;

    public User getUserByToken(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("authorization");
        String userId;
        try {
           userId = JwtUtil.parseJWT(token).get("id").toString();
        } catch (Exception exception) {
            System.out.println("认证失败");
            return null;
        }
        return userService.findUserById(userId);
    }
}
